package structures;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import exception.IBToolsException;
import manager.LoadNativeTableManager;
import manager.ScreenDAOManager;
import manager.SessionManager;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SelectableDataModel;
import org.primefaces.model.SortOrder;

/**
 * Dummy implementation of LazyDataModel that uses a list to mimic a real datasource like a database.
 */
public class LazyLogServiceCallDAODataModel extends LazyDataModel<HistoryData> implements SelectableDataModel<HistoryData> {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -4409931033650685587L;
	
	private List<HistoryData> datasource;

	private String historyServiceNameFilter;
	private String historyCallDateFilter;
	
    public LazyLogServiceCallDAODataModel(String historyServiceNameFilter,
			String historyCallDateFilter) {
		super();
		this.historyServiceNameFilter = historyServiceNameFilter;
		this.historyCallDateFilter = historyCallDateFilter;
	}

	@Override
    public HistoryData getRowData(String rowKey) {
        for(HistoryData history : datasource) {
            if(history.getOid().longValue() == new Long(rowKey).longValue())
                return history;
        }
        return null;
    }

    @Override
    public Object getRowKey(HistoryData history) {
        return history.getOid();
    }

    @Override
    public List<HistoryData> load(int start, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters){
        try {
//			datasource = ScreenDAOManager.loadHistory(start, pageSize, "callDatetimestamp", SortOrder.DESCENDING, 
//				historyServiceNameFilter, historyCallDateFilter, SessionManager.getLoginUserCode(), -1);

			
        	datasource = LoadNativeTableManager.loadHistoryNative(start, pageSize, "callDatetimestamp", SortOrder.DESCENDING, 
					historyServiceNameFilter, historyCallDateFilter, SessionManager.getLoginUserCode(), -1);
			
//			for (LogServiceCallDAO logServiceCallDAO : datasource) {
//				logServiceCallDAO.loadOutSampleMessage();
//			}
	        setRowCount(ScreenDAOManager.loadHistoryCount(start, pageSize, "callDatetimestamp", SortOrder.DESCENDING, historyServiceNameFilter, 
	        	historyCallDateFilter, SessionManager.getLoginUserCode(), -1));
		} catch (IBToolsException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return datasource;
    }
    
    @Override
    public void setRowIndex(int rowIndex) {
    	// TODO Auto-generated method stub
    	if(this.getPageSize() != 0)
    		super.setRowIndex(rowIndex);
    }
}
                    